package com.asiainfo.integration.actions;

import org.apache.log4j.Logger;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/11/19
 */
@WebServlet(urlPatterns = "/message/count", asyncSupported = true)
public class CountMsgAction extends HttpServlet {

    public static final Logger INFO_LOG = Logger.getLogger(CountMsgAction.class);

    private static final String INITIAL_CONTEXT_FACTORY = "com.tibco.tibjms.naming.TibjmsInitialContextFactory";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String connection_url = request.getParameter("connection_url").trim();
        String connection_factory = request.getParameter("connection_factory").trim();
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        String queue_name = request.getParameter("queue_name").trim();
        String message_content = request.getParameter("message_content");
        String rollback_mark = request.getParameter("rollback_mark");
        String interfaceName = request.getParameter("interface_name");

        request.setAttribute("connection_url", connection_url);
        request.setAttribute("connection_factory", connection_factory);
        request.setAttribute("username", username);
        request.setAttribute("password", password);
        request.setAttribute("queue_name", queue_name);
        request.setAttribute("message_content", message_content);
        request.setAttribute("interface_name", interfaceName);

        try {
            Properties tibcoProperties = new Properties();
            tibcoProperties.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
            tibcoProperties.put(Context.PROVIDER_URL, connection_url);
            tibcoProperties.put(Context.SECURITY_PRINCIPAL, username);
            tibcoProperties.put(Context.SECURITY_CREDENTIALS, password);
            Context context = new InitialContext(tibcoProperties);
            QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup(connection_factory);
            Connection connection = factory.createConnection(username, password);
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queue_name);

            connection.start();

            int msgCnt = 0;

            QueueBrowser browser = session.createBrowser(queue);

            for(Enumeration messages = browser.getEnumeration(); messages.hasMoreElements(); ++msgCnt) {
                messages.nextElement();
            }

            browser.close();

            connection.stop();

            request.setAttribute("is_error", false);
            SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
            request.setAttribute("error_message", formatter.format(new Date()) +" All Message are accounted for! Total: " + msgCnt);

            request.getRequestDispatcher("/WEB-INF/pages/count.jsp").forward(request, response);
        } catch (NamingException e) {
            e.printStackTrace();
            request.setAttribute("error_message", e.getMessage());
            request.setAttribute("is_error", true);
            request.getRequestDispatcher("/WEB-INF/pages/count.jsp").forward(request, response);
        } catch (JMSException e) {
            e.printStackTrace();
            request.setAttribute("error_message", e.getMessage());
            request.setAttribute("is_error", true);
            request.getRequestDispatcher("/WEB-INF/pages/count.jsp").forward(request, response);
        }
    }

}
